// 云函数入口文件
const cloud = require('wx-server-sdk')
const mysql = require('mysql2/promise')

cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
  var id = event.id;
  console.log(event)
  try {
    const connection = await mysql.createConnection({
      host: "47.115.22.254",
      port: 3306,
      database: "EPMS",
      user: "root",
      password: "1234"
    })
    var outsql = 'SELECT * FROM OUTPEOPLE WHERE userid = "' + id + '";';
    var studentsql = 'SELECT * FROM SUSER WHERE studentID = "' + id + '";';
    var teachersql = 'SELECT * FROM TUSER WHERE teacherID = "' + id + '";';
    var [studentrows, studentfields] = await connection.execute(studentsql);
    
    

   if(studentrows.length == 0){
     var [teacherrows, teacherfields] = await connection.execute(teachersql);
     if(teacherrows.length == 0){
       var [outrows, outfields] = await connection.execute(outsql);
       return outrows
     }else{
       return teacherrows
     }  
   }else{
     return studentrows
   }

    conection.end();
  } catch (err) {
    return err
  }
}